package com.usernet.product.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.usernet.product.comm.BaseDao;
import com.usernet.product.entity.LoginError;

public class LoginErrorDao extends BaseDao {


	/**
	 * 清除错误登录信息
	 * @param loginName 登录名
	 * @param flag 登录标识1:管理后台登录;2:网站登录;3:手机端登录
	 */
	public void cleanLoginError(String loginName, int flag) {
		Session session = getSession();
		String hql = "delete LoginError where loginName=:loginName and flag=:flag";
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setString("loginName", loginName);
			query.setInteger("flag", flag);
			query.executeUpdate();
			tr.commit();
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
	}
	/**
	 * 查询登录失败信息
	 * 
	 * @param loginName
	 *            登录名
	 * @param ip
	 *            登录IP
	 * @param flag
	 *            登录标识1:管理后台登录;2:网站登录;3:手机端登录
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public LoginError getByParam(String loginName, String ip, int flag) {
		LoginError loginError = null;
		Session session = getSession();
		String hql = "from LoginError where loginName=:loginName and ip=:ip and flag=:flag";
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setString("loginName", loginName);
			query.setString("ip", ip);
			query.setInteger("flag", flag);
			List list = query.list();
			if (list.size() > 0) {
				loginError = (LoginError) list.get(0);
			}
			tr.commit();
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return loginError;
	}

}
